package com.andyliu.sys.controller;

import com.andyliu.sys.pojo.SRole;
import com.andyliu.sys.service.RoleService;
import com.andyliu.vo.ParamVO;
import com.andyliu.vo.Result;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: RoleController
 * Description: TODO
 * date: 2021/1/20 5:18 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@RestController
@RequestMapping("sys/role")
@Api(tags = "角色管理请求",produces = "application/json",protocols = "http")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("save")
    @ApiOperation(value = "保存角色信息",
            notes = "角色的insert和update",
            httpMethod = "POST")
    public Result saveRole(@ApiParam(name = "请求用户的相关信息",
            required = true,
            value = "属性序列化后的值") @RequestBody  SRole role) {
        Boolean status = roleService.saveRole(role);
        Result result = Result.ok(status);
        return result;
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询角色信息",
            notes = "根据id查询角色信息",
            httpMethod = "GET")
    public Result<Object> selectRole(@ApiParam(name = "id", value = "120", required = true) @PathVariable Integer id) {
        SRole role = roleService.selectRoleById(id);
        Result<Object> rs = Result.ok("",role);
        return rs;
    }

    @PostMapping("query")
    @ApiOperation(notes = "角色分页查询",
            value = "分页根据条件角色查询",
            httpMethod = "GET")
    public Result<Object> selectRoles(
            @ApiParam(name = "角色查询信息")
            @RequestBody ParamVO<SRole> role) {
        PageInfo<SRole> page = roleService.selectRoleByPage(role.getParam(), role.getPageNumber(), role.getPageSize());
        Result<Object> rs = Result.ok("",page);
        return rs;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(notes = "根据id删除角色信息",
            value = "根据删除角色信息",
            httpMethod = "DELETE")
    public Result<Object> deleteRole(
            @ApiParam(name = "id", value = "id", defaultValue = "688", required = true)
            @RequestParam(required = true) Integer id) {
        Boolean status = roleService.deleteRoleById(id);
        Result<Object> rs = Result.ok(status);
        return rs;
    }

    @DeleteMapping("/deletes")
    @ApiOperation(notes = "批量删除",
            value = "根据id批量删除",
            httpMethod = "DELETE")
    public Result<Object> deleteRoles(
            @ApiParam(name = "ids", value = "需要批量删除的id数组") Integer[] ids) {
        Boolean status = roleService.deleteRoleById(ids);
        Result<Object> rs = Result.ok(status);
        return rs;
    }

    @PostMapping("/setRoleEnabled")
    @ApiOperation(value = "设置角色启用和停用",
            notes = "设置角色启用停用",
            httpMethod = "POST")
    public Result<Object> setRoleEnabled(
            @ApiParam(name = "id", value = "需要启用｜停用的id")
            @RequestParam(required = true) Integer id,
            @ApiParam(name = "status", value = "需要启用或停用")
            @RequestParam(required = true) Integer status) {
        SRole role = new SRole();
        role.setId(id);
        role.setStatus(status);
        Boolean flag = roleService.saveRole(role);
        Result<Object> rs = Result.ok(flag);
        return rs;
    }

    @GetMapping
    @ApiOperation(value = "查询所有角色",produces = "application/json")
    public Result<Object> queryRoles(){
        List<SRole> roles = roleService.queryRoles();
        return Result.ok("",roles);
    }
    @ApiOperation(value = "查询角色拥有的权限",produces = "application/json")
    @GetMapping("role/resourceJsonData")
    @ResponseBody
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public Result<Object> roleResourceJsonData(Integer roleId){
        List list = roleService.selectRoleResources_(roleId);
        return Result.ok("",list);
    }
    @ApiOperation(value = "保存角色权限信息")
    @GetMapping("savePermission")
    @ResponseBody
    public Result<Object> savePermission(Integer[] resourceIds,Integer roleId){
        Boolean status = roleService.saveRolePermission(roleId,resourceIds);
        return Result.ok(status);
    }
}
